package net.anzhuo.controller.db;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;
import net.anzhuo.common.utils.DateUtil;
import net.anzhuo.common.utils.RandomGUID;
import net.anzhuo.common.utils.RedisUtil;
import net.anzhuo.entity.GsBase;
import net.anzhuo.entity.GsBranch;
import net.anzhuo.entity.GsChange;
import net.anzhuo.entity.GsGudong;
import net.anzhuo.entity.GsMember;
import net.anzhuo.entity.GsOuttouzi;
import net.anzhuo.entity.GsPriority;
import net.anzhuo.entity.GsQuery;
import net.anzhuo.entity.db.ChangeRecords;
import net.anzhuo.entity.db.basicinfo.Branches;
import net.anzhuo.entity.db.basicinfo.DetailResult;
import net.anzhuo.entity.db.basicinfo.Partners;
import net.anzhuo.entity.db.basicinfo.Result;
import net.anzhuo.service.GsBaseService;
import net.anzhuo.service.GsBranchService;
import net.anzhuo.service.GsChangeService;

import net.anzhuo.service.GsGudongService;
import net.anzhuo.service.GsMemberService;
import net.anzhuo.service.GsOuttouziService;

import net.anzhuo.service.GsPriorityService;
import net.anzhuo.service.GsQueryService;


@RestController
@Slf4j
public class DetailSimpleController {

	@Autowired
	private GsBaseService gsBaseService;
	@Autowired
	private GsGudongService gsGudongService;
	
	
	@Autowired
	private GsOuttouziService gsOuttouziService;
	
	
	@Autowired
	private GsMemberService gsMemberService;

	@Autowired
	private GsBranchService gsBranchService;
	@Autowired
	private GsChangeService gsChangeService;

	@Autowired
	private GsPriorityService gsPriorityService;
	
	
	@Autowired
	private GsQueryService gsQueryService;

	

	

	
	

	


	
	
	@RequestMapping("/BasicInfo")
	public DetailResult GetBasicInfo(String keyword) {
		log.info("===>BasicInfo_keyword="+keyword);
	
		long startTime=System.currentTimeMillis();
		
		DetailResult detailResult = new DetailResult();
		try {
	
		detailResult.setStatus("200");
		detailResult.setMessage("查询成功");
		
		Result result = new Result();
		GsBase gsBase = gsBaseService.getByCreditCode(keyword);
		if(gsBase==null)
		{	
		gsBase=new GsBase();
		GsBase gg=gsBaseService.getByName(keyword);
		String md5=RandomGUID.generatorGUID();
		gsBase.setMd5(md5);
		if(gg==null)
		{	
			GsQuery gs=new GsQuery();
			gs.setKeyNo(md5);
			gs.setStatus("0");
			gs.setCreditCode(keyword);
		    gs.setUpdateTime(DateUtil.getDate("yyyy-MM-dd"));		
		    
		    List<GsQuery> listQuery=gsQueryService.getByKeyNo(gs);
		    if(listQuery.size()>0)
		    {
		    	gs.setId(listQuery.get(0).getId());
		    	gsQueryService.update(gs);
		    }else
		    {
			gsQueryService.save(gs);
		    }
		    //waiting for spiderman
		    listQuery=gsQueryService.getByKeyNo(gs);
		    if(listQuery.size()>0)
		    {
		    	gs=listQuery.get(0);
		    	int count=1;
		    	while(gs.getStatus().equals("0")&&count<1000)
		    	{
		    		 listQuery=gsQueryService.getByKeyNo(gs);
		    		 if(listQuery.size()>0)
		    		 {
		    			 gs=listQuery.get(0);
		    		 }
		    		 Thread.currentThread().sleep(10000);
		    		 count++;
		    	}
		    }
		}
		
		
		}else {
			
			
			GsPriority gp=new GsPriority();
			gp.setKeyNo(gsBase.getMd5());
			gp.setStatusBasic("0");
			gp.setOperdate(DateUtil.formatDate(DateUtils.addDays(new Date(), 0),"yyyy-MM-dd"));
			List<GsPriority> listPriority=gsPriorityService.getByKeyNo(gp);
			if(listPriority.size()>0)
			{
			gsPriorityService.update(gp);
			}else
			{
				gsPriorityService.save(gp);
			}
			//waiting for update
			 listPriority=gsPriorityService.getByKeyNo(gp);
			 log.info("waiting for update:listPriority's size="+listPriority.size());
			 int i=1;
			if(listPriority.size()>0)
			{
				 gp=listPriority.get(0);
				 log.info("statusBasic="+gp.getStatusBasic());
				 while(gp.getStatusBasic().equals("0")&&i<1000)
				 {
					 listPriority=gsPriorityService.getByKeyNo(gp);
					 if(listPriority.size()>0)
					 {
						 gp=listPriority.get(0);
					 }
					 Thread.currentThread().sleep(10000);
					 i++;
				 }
			}
		result.setKeyNo(gsBase.getMd5());
		result.setName(gsBase.getName());
		result.setBelongOrg(gsBase.getRegAuth());
		result.setOperName(gsBase.getLegal());
		result.setStartDate(gsBase.getRegTime());
	    result.setEndDate(gsBase.getApprovalTime());
		result.setStatus(gsBase.getEnterpriseStatus());
		result.setProvince(gsBase.getProvince());
		result.setCreditCode(gsBase.getCreditCode());
		result.setRegistCapi(gsBase.getRegZb());
		result.setEconKind(gsBase.getEnterpriseType());
		result.setAddress(gsBase.getRegAddress());
		result.setScope(gsBase.getOperScope());
		result.setTermStart(gsBase.getBusinessStartTime());
		result.setTeamEnd(gsBase.getBusinessEndTime());
		result.setCheckDate(gsBase.getApprovalTime());
		result.setOrgNo(gsBase.getDeptCode());
		log.info("deptcode="+gsBase.getDeptCode());
		result.setNo(gsBase.getRegNumber());
		log.info("regno="+gsBase.getRegNumber());
		List<GsGudong> gudongList = gsGudongService.getByMd5(gsBase.getMd5());
		if (gudongList!=null && gudongList.size()>0) {
			List<Partners> partnersList = new ArrayList<>();
			Partners partners = null;
			for (GsGudong g : gudongList) {
				partners = new Partners();
				partners.setStockName(g.getShareholder());
				partners.setStockPercent(g.getCapitalPercent());
				partners.setShouldCapi(g.getConfisCapital());
				partners.setShoudDate(g.getCapitaltime());
				partners.setStockType(g.getCapitaltype());
				partnersList.add(partners);
			}
			Collections.sort(partnersList, new Comparator<Partners>() {
	            public int compare(Partners o1, Partners o2) {
	                return o1.getStockPercent().replace("%","").compareTo(o2.getStockPercent().replace("%",""));
	            }
	        });
			
			result.setPartners(partnersList);
		}else
		{
			List<Partners> partnersList = new ArrayList<>();
			result.setPartners(partnersList);
		}
		
		
		
		List<GsBranch> branchList = gsBranchService.getByMd5(gsBase.getMd5());
		if (branchList!=null && branchList.size()>0) {
			List<Branches> branchesList = new ArrayList<>();
			Branches branches = null;
			for (GsBranch b : branchList) {
				
				GsBase branch = gsBaseService.getByMd5(b.getMd5());
				branches = new Branches();
				
				branches.setName(b.getEnterpriseName());
				branches.setCompanyId(b.getMd5());
				if(branch!=null)
				{
				branches.setCreditCode(branch.getCreditCode());
				branches.setRegNo(branch.getRegNumber());
				branches.setOperName(branch.getLegal());
				branches.setBelongOrg(branch.getRegAuth());
				}
				branchesList.add(branches);
			}
			result.setBranches(branchesList);
		}else
		{
			List<Branches> branchesList = new ArrayList<>();
			result.setBranches(branchesList);
		}
		
		List<GsChange> changeList = gsChangeService.getByMd5(gsBase.getMd5());
		if (changeList!=null && changeList.size()>0) {
			List<ChangeRecords> changeRecordsList = new ArrayList<>();
			ChangeRecords changeRecords = null;
			for (GsChange c : changeList) {
				changeRecords = new ChangeRecords();
				changeRecords.setProjectName(c.getChangeProject());
				changeRecords.setBeforeContent(c.getChangeBefore());
				changeRecords.setAfterContent(c.getChangeAfter());
				changeRecords.setChangeDate(c.getChangeTime());
				changeRecordsList.add(changeRecords);
			}
			result.setChangeRecords(changeRecordsList);
		}else
		{
			List<ChangeRecords> changeRecordsList = new ArrayList<>();
			result.setChangeRecords(changeRecordsList);
		}
		}
		detailResult.setResult(result);
		}catch(Exception e1)
		{
			log.info(e1.getMessage());
		}
		
		
		 long endTime=System.currentTimeMillis();
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info(" BasicInfo_keyword 执行时间："+excTime+"s");
		return detailResult;
	}
	
	
	@RequestMapping("/UpdateBasicInfo")
	public net.anzhuo.entity.db.syncdata.Result UpdateBasicInfo(String keyNo,String name,String regAddress,
			String webSite,
			String regZb,
			String regStatus,
			String regTime,
			String creditCode,
			String taxNumber,
			String regNumber,
			String orgCode,
			String regType,
			String industry,
			String approvalTime,
			String regAuth,
			String province,
			String businessStart,
			String businessEnd,
			String regScope,
			String updateTime,
			String legal
			) {
		log.info("===>UpdateBasicInfo_keyword="+keyNo+",name="+name+",regAddress="+regAddress+",webSite="+webSite+",regZb="+regZb+",regStatus="+regStatus+",regTime="+regTime+",creditCode="+creditCode+",regNumber="+regNumber+",orgCode="+orgCode+",regType="+regType+",industry="+industry+",approvalTime="+approvalTime+",regAuth="+regAuth+",province="+province+",businessStart="+businessStart+",businessEnd="+businessEnd+",regScope="+regScope+",updateTime="+updateTime);
		net.anzhuo.entity.db.syncdata.Result result=new net.anzhuo.entity.db.syncdata.Result();
		long startTime=System.currentTimeMillis();
		GsBase gsBase=new GsBase();
		gsBase.setMd5(keyNo);
		gsBase.setName(name);
		gsBase.setRegAddress(regAddress);
		gsBase.setWebSite(webSite);
		gsBase.setRegZb(regZb);
		gsBase.setEnterpriseStatus(regStatus);
		gsBase.setRegTime(regTime);
		gsBase.setCreditCode(creditCode);
		gsBase.setRegNumber(regNumber);
		gsBase.setDeptCode(orgCode);
		gsBase.setEnterpriseType(regType);
		gsBase.setIndustry(industry);
		gsBase.setApprovalTime(approvalTime);
		gsBase.setRegAuth(regAuth);
		gsBase.setProvince(province);
		gsBase.setBusinessStartTime(businessStart);
		gsBase.setBusinessEndTime(businessEnd);
		gsBase.setOperScope(regScope);
		gsBase.setUpdateTime(updateTime);
		gsBase.setLegal(legal);
		if(regTime!=null&&!regTime.equals("")&&regTime.length()==10)
		{
		gsBase.setYear(regTime.substring(0, 4));
		}
		GsBase gb=gsBaseService.getByMd5(keyNo);
		
		if(gb!=null)
		{
			gsBase.setId(gb.getId());
			gsBase.setMd5(gb.getMd5());
			log.info("id="+gb.getId());
			gsBaseService.update(gsBase);
			log.info("keyNo already exists."+gb.getMd5());
		}else
		{
			boolean isExists=false;
			if(gsBase.getCreditCode()!=null&&!gsBase.getCreditCode().equals("")&&gsBase.getCreditCode().trim().length()==18)
			{
			GsBase gs=gsBaseService.getByCreditCode(creditCode);
			if(gs!=null)
			{
				isExists=true;
				gsBase.setId(gs.getId());
				gsBase.setMd5(gs.getMd5());
			}
			}
			//英文字符()
			if(isExists==false)
			{
			String sCorpName=gsBase.getName().trim();
			sCorpName=sCorpName.replace("（", "(");
			sCorpName=sCorpName.replace("）", ")");
			GsBase g11=gsBaseService.getByName(sCorpName);
			if(g11!=null)
			{
				isExists=true;
				gsBase.setId(g11.getId());
				gsBase.setMd5(g11.getMd5());
			}else
			{
				sCorpName=sCorpName.replace("(", "（");
				sCorpName=sCorpName.replace(")", "）");
				GsBase g112=gsBaseService.getByName(sCorpName);
				if(g112!=null)
				{
					isExists=true;
					gsBase.setId(g112.getId());
					gsBase.setMd5(g112.getMd5());
				}
			}			
			}
			if(isExists)
			{
				gsBaseService.update(gsBase);
				GsPriority gp=new GsPriority();
				gp.setKeyNo(gsBase.getMd5());
				gp.setStatus("0");
			List<GsPriority> prioritylist=	gsPriorityService.getBycoMd5(gp);
			if(prioritylist.size()==0)
			{
				gsPriorityService.save(gp);
			}else
			{
				gsPriorityService.update(gp);
			}
			//update gsquery
			GsQuery gsQuery=new GsQuery();
			gsQuery.setKeyNo(gsBase.getMd5());
			List<GsQuery> gsQueryList=gsQueryService.getBycoMd5(gsQuery);
			if(gsQueryList.size()>0)
			{
				gsQuery.setStatus("1");
				gsQuery.setUpdateTime(DateUtil.getDate("yyyy-MM-dd"));
				gsQueryService.update(gsQuery);
			}
			}else
			{
				log.info(" not exists saved"+gsBase.getMd5());
				gsBaseService.save(gsBase);
			}
			
		}
			
		result.setStatus("200");
		result.setMessage("success");
		 long endTime=System.currentTimeMillis();
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info(" UpdateBasicInfo_keyword 执行时间："+excTime+"s");
		return result;
	}
	
	@RequestMapping("/DeleteStockInfo")
	public net.anzhuo.entity.db.syncdata.Result DeleteStockInfo(String keyNo
			) {
		log.info("===>DeleteStockInfo_keyword="+keyNo);
		net.anzhuo.entity.db.syncdata.Result result=new net.anzhuo.entity.db.syncdata.Result();
		long startTime=System.currentTimeMillis();
		GsGudong gsGudong=new GsGudong();
		gsGudong.setCoMd5(keyNo);
	
		gsGudongService.delete(gsGudong);
		
		result.setStatus("200");
		result.setMessage(" DeleteStockInfo success");
		long endTime=System.currentTimeMillis();
		float excTime=(float)(endTime-startTime)/1000;
		log.info("DeleteStockInfo_keyword 执行时间："+excTime+"s");
		return result;
	}
	
	
	
	@RequestMapping("/UpdateStockInfo")
	public net.anzhuo.entity.db.syncdata.Result UpdateStockInfo(String keyNo,String shareholder,
			String capitalPercent,
			String capitalTime,
			String confisCapital,
			String updateTime
			) {
		log.info("===>UpdateStockInfo_keyword="+keyNo+",shareholder="+shareholder+",capitalPercent="+capitalPercent+",capitalTime="+capitalTime+",confisCapital="+confisCapital+",updateTime="+updateTime);
		net.anzhuo.entity.db.syncdata.Result result=new net.anzhuo.entity.db.syncdata.Result();
		long startTime=System.currentTimeMillis();
		GsGudong gsGudong=new GsGudong();
		gsGudong.setCoMd5(keyNo);
		gsGudong.setShareholder(shareholder);
		gsGudong.setCapitalPercent(capitalPercent);
		gsGudong.setCapitaltime(capitalTime);
		gsGudong.setConfisCapital(confisCapital);
		gsGudong.setUpdateTime(updateTime);
		if(shareholder.contains("公司"))
        {
        gsGudong.setCapitaltype("1");
	    }else
	    {
	    	 gsGudong.setCapitaltype("0");
	    }

		List<GsGudong> list=gsGudongService.getByMd5(keyNo);
		String md5=RandomGUID.generatorGUID();
		boolean flag=false;
		boolean flagMd5=false;
		for(GsGudong g:list)
		{
			if(g.getShareholder()!=null)
			{
			if(g.getShareholder().equals(gsGudong.getShareholder()))
			{
				flag=true;
				gsGudong.setMd5(g.getMd5());
				gsGudong.setId(g.getId());
			}
			}
			
			if(g.getMd5()!=null)
			{
				if(g.getMd5().equals(gsGudong.getMd5()))
				{
					flagMd5=true;
					
				}
			}
		}
		
		if(flag)
		{
			gsGudongService.update(gsGudong);
		}else
		{
			
			GsBase gs=gsBaseService.getByName(gsGudong.getShareholder());
			if(gs==null)
			{
			if(flagMd5)
			{
				md5=RandomGUID.generatorGUID();
				gsGudong.setMd5(md5);
			}else
			{
				gsGudong.setMd5(md5);
			}
			}else
			{
				gsGudong.setMd5(gs.getMd5());
			}
			gsGudongService.save(gsGudong);
		}
			
		result.setStatus("200");
		result.setMessage("success");
		long endTime=System.currentTimeMillis();
		float excTime=(float)(endTime-startTime)/1000;
		log.info("UpdateStockInfo_keyword 执行时间："+excTime+"s");
		return result;
	}
	
	
	@RequestMapping("/DeleteOutCapitalInfo")
	public net.anzhuo.entity.db.syncdata.Result DeleteOutCapitalInfo(String keyNo
			) {
		log.info("===>DeleteOutCapitalInfo_keyword="+keyNo);
		net.anzhuo.entity.db.syncdata.Result result=new net.anzhuo.entity.db.syncdata.Result();
		long startTime=System.currentTimeMillis();
		GsOuttouzi gsOuttouzi=new GsOuttouzi();
		
		
		gsOuttouziService.delete(gsOuttouzi);
		
			
		result.setStatus("200");
		result.setMessage("DeleteOutCapitalInfo success");
		 long endTime=System.currentTimeMillis();
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info("UpdateOutCapitalInfo_keyword 执行时间："+excTime+"s");
		return result;
	}
	
	@RequestMapping("/UpdateOutCapitalInfo")
	public net.anzhuo.entity.db.syncdata.Result UpdateOutCapitalInfo(String keyNo,
			String enterpriseName,
			String legal,
			String regCap,
			String regPercent,
			String regTime,
			String updateTime,
			String state
			) {
		log.info("===>UpdateOutCapitalInfo_keyword="+keyNo+",enterpriseName="+enterpriseName+",legal="+legal+",regCap="+regCap+",regPercent="+regPercent+",regTime="+regTime+",updateTime="+updateTime+",state="+state);
		net.anzhuo.entity.db.syncdata.Result result=new net.anzhuo.entity.db.syncdata.Result();
		long startTime=System.currentTimeMillis();
		GsOuttouzi gsOuttouzi=new GsOuttouzi();
		gsOuttouzi.setEnterpriseName(enterpriseName);
		gsOuttouzi.setLegal(legal);
		gsOuttouzi.setRegCapital(regCap);
		gsOuttouzi.setInvestmentRatio(regPercent);
		gsOuttouzi.setRegTime(regTime);
		gsOuttouzi.setCoMd5(keyNo);
		gsOuttouzi.setUpdateTime(updateTime);
		gsOuttouzi.setState(state);
		

		List<GsOuttouzi> list=gsOuttouziService.getList(gsOuttouzi);
		boolean flag=false;
		String md5=RandomGUID.generatorGUID();
		for(GsOuttouzi g:list)
		{
			if(g.getEnterpriseName()!=null)
			{
			if(g.getEnterpriseName().equals(gsOuttouzi.getEnterpriseName()))
			{
				flag=true;
				gsOuttouzi.setMd5(g.getMd5());
				gsOuttouzi.setId(g.getId());
				gsOuttouzi.setLegal(g.getLegal());
				
			}
			}
			
			
		}
		
		if(flag)
		{
			gsOuttouziService.update(gsOuttouzi);
		}else
		{   
		 GsBase	gsBase=gsBaseService.getByName(enterpriseName);
		 if(gsBase!=null)
		 {
		 gsOuttouzi.setMd5(gsBase.getMd5());
		 gsOuttouzi.setState(gsBase.getEnterpriseStatus());
		 }else
		 {
			 gsOuttouzi.setMd5(md5);
		 }
			gsOuttouziService.save(gsOuttouzi);
		}
			
		result.setStatus("200");
		result.setMessage("UpdateOutCapitalInfo success");
		 long endTime=System.currentTimeMillis();
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info("UpdateOutCapitalInfo_keyword 执行时间："+excTime+"s");
		return result;
	}
	
	
	@RequestMapping("/DeleteMemberInfo")
	public net.anzhuo.entity.db.syncdata.Result DeleteMemberInfo(String keyNo
			) {
		log.info("===>DeleteMemberInfo_keyword="+keyNo);
		net.anzhuo.entity.db.syncdata.Result result=new net.anzhuo.entity.db.syncdata.Result();
		long startTime=System.currentTimeMillis();
		GsMember gm=new GsMember();
		gm.setCoMd5(keyNo);
	
		gsMemberService.delete(gm);
	
		
			
		result.setStatus("200");
		result.setMessage("Delete member success");
		 long endTime=System.currentTimeMillis();
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info("DeleteMemberInfo_keyword 执行时间："+excTime+"s");
		return result;
	}
	
	
	@RequestMapping("/UpdateMemberInfo")
	public net.anzhuo.entity.db.syncdata.Result UpdateMemberInfo(String keyNo,
			String name,
			String position,
			String updateTime,
			String md5
			) {
		log.info("===>UpdateMemberInfo_keyword="+keyNo+",name="+name+",position="+position+",updateTime="+updateTime);
		net.anzhuo.entity.db.syncdata.Result result=new net.anzhuo.entity.db.syncdata.Result();
		long startTime=System.currentTimeMillis();
		GsMember gm=new GsMember();
		gm.setCoMd5(keyNo);
		gm.setName(name);
		gm.setPosition(position);
		gm.setUpdateTime(updateTime);

		List<GsMember> list=gsMemberService.getList(gm);
		
		boolean flag=false;
		for(GsMember g:list)
		{
			if(g.getName()!=null)
			{
			if(g.getName().equals(gm.getName()))
			{
				flag=true;
				gm.setMd5(g.getMd5());
				gm.setId(g.getId());
			}
			}
			
			
		}
		
		if(flag)
		{
			gsMemberService.update(gm);
		}else
		{   
			gm.setMd5(md5);
			gsMemberService.save(gm);
		}
			
		result.setStatus("200");
		result.setMessage("UpdateMemberInfo success");
		 long endTime=System.currentTimeMillis();
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info("UpdateMemberInfo_keyword 执行时间："+excTime+"s");
		return result;
	}
	
	@RequestMapping("/DeleteBranchInfo")
	public net.anzhuo.entity.db.syncdata.Result DeleteBranchInfo(String keyNo
			) {
		log.info("===>DeleteBranchInfo_keyword="+keyNo);
		net.anzhuo.entity.db.syncdata.Result result=new net.anzhuo.entity.db.syncdata.Result();
		long startTime=System.currentTimeMillis();
		GsBranch gm=new GsBranch();
		gm.setCoMd5(keyNo);
		
	    gsBranchService.delete(gm);
		
		
		
		result.setStatus("200");
		result.setMessage("delete BranchList success");
		 long endTime=System.currentTimeMillis();
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info("DeleteBranchInfo_keyword 执行时间："+excTime+"s");
		return result;
	}
	
	
	
	
	@RequestMapping("/UpdateBranchInfo")
	public net.anzhuo.entity.db.syncdata.Result UpdateBranchInfo(String keyNo,
			String name,
			String updateTime
			) {
		log.info("===>UpdateBranchInfo_keyword="+keyNo+",name="+name+",updateTime="+updateTime);
		net.anzhuo.entity.db.syncdata.Result result=new net.anzhuo.entity.db.syncdata.Result();
		long startTime=System.currentTimeMillis();
		GsBranch gm=new GsBranch();
		gm.setCoMd5(keyNo);
		gm.setEnterpriseName(name);
	    gm.setUpdateTime(updateTime);
		List<GsBranch> list=gsBranchService.getList(gm);
		String md5=RandomGUID.generatorGUID();
		boolean flag=false;
		for(GsBranch g:list)
		{
			if(g.getEnterpriseName()!=null)
			{
			if(g.getEnterpriseName().equals(gm.getEnterpriseName()))
			{
				flag=true;
				gm.setMd5(g.getMd5());
				gm.setId(g.getId());
			}
			}
			
			
		}
		
		if(flag)
		{
			gsBranchService.update(gm);
		}else
		{   GsBase	gsBase=gsBaseService.getByName(gm.getEnterpriseName());
		    if(gsBase!=null)
		    {
	        gm.setLegal(gsBase.getLegal());
	        gm.setRegTime(gsBase.getRegTime());
	        gm.setState(gsBase.getEnterpriseStatus());
	        gm.setMd5(gsBase.getMd5());
		    }else
		    {
		    	gm.setMd5(md5);
		    }
			gsBranchService.save(gm);
		}
			
		result.setStatus("200");
		result.setMessage(" update branchlist success");
		 long endTime=System.currentTimeMillis();
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info("UpdateBranchInfo_keyword 执行时间："+excTime+"s");
		return result;
	}
	
	
	
	@RequestMapping("/DeleteChangeInfo")
	public net.anzhuo.entity.db.syncdata.Result UpdateChangeInfo(String keyNo
			) {
		log.info("===>DeleteChangeInfo_keyword="+keyNo);
		net.anzhuo.entity.db.syncdata.Result result=new net.anzhuo.entity.db.syncdata.Result();
		long startTime=System.currentTimeMillis();
		GsChange gm=new GsChange();
		gm.setCoMd5(keyNo);
	
        gsChangeService.delete(gm);
		
		result.setStatus("200");
		result.setMessage("DeleteChangeInfo success");
		 long endTime=System.currentTimeMillis();
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info("DeleteChangeInfo_keyword 执行时间："+excTime+"s");
		return result;
	}
	
	@RequestMapping(value="/UpdateChangeInfo",method = RequestMethod.POST)
	public net.anzhuo.entity.db.syncdata.Result UpdateChangeInfo(String keyNo,
			String changeTime,
			String changeProject,
			String changeBefore,
			String changeAfter,
			String updateTime
			) {
		log.info("===>UpdateChangeInfo_keyword="+keyNo+",changeTime="+changeTime+",changeProject="+changeProject+",changeBefore="+changeBefore+",changeAfter="+changeAfter+",updateTime="+updateTime);
		net.anzhuo.entity.db.syncdata.Result result=new net.anzhuo.entity.db.syncdata.Result();
		long startTime=System.currentTimeMillis();
		GsChange gm=new GsChange();
		gm.setCoMd5(keyNo);
		gm.setChangeTime(changeTime);
		gm.setChangeBefore(changeBefore);
		gm.setChangeAfter(changeAfter);
		gm.setChangeProject(changeProject);
        gm.setUpdateTime(updateTime);
        
 
		List<GsChange> list=gsChangeService.getList(gm);
		String md5=RandomGUID.generatorGUID();
		boolean flag=false;
		for(GsChange g:list)
		{
			if(g.getChangeProject()!=null)
			{
			if(g.getChangeProject().equals(gm.getChangeProject())&&g.getChangeTime().equals(gm.getChangeTime())&&g.getChangeBefore().equals(gm.getChangeBefore())&&g.getChangeAfter().equals(gm.getChangeAfter()))
			{
				flag=true;
				gm.setMd5(g.getMd5());
				gm.setId(g.getId());
			}
			}	
		}
		
		if(flag)
		{
			gsChangeService.update(gm);
		}else
		{   
		   gm.setMd5(md5);	    
		   gsChangeService.save(gm);
		}			
		result.setStatus("200");
		result.setMessage("update changelist success");
		 long endTime=System.currentTimeMillis();
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info("UpdateChangeInfo_keyword 执行时间："+excTime+"s");
		return result;
	}
	
}
